package model;

import gui.MainFrame;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 
 * @author Csaba Tarkanyi
 */
public class Reparation {

	private long uniqueID;
	private GregorianCalendar startDate;
	private GregorianCalendar endDate;
	private String description;
	// link: Reparation 0...*--1 Machine
	private Machine machine = null;
	// link: Reparation 0...1-->0...* SparePartHistory
	private List<SparePartHistory> sparePartHistories;
	// The History for the Reparation (it will be useful for mostly for the
	// RepairTypes as a Reparation does not contains this, only SpareParts - The
	// reason is if a Reparation contains RepairTypes is causes problems when
	// the RepairType will be changed (add or remove SpareParts) -> the History
	// of the Reparation will
	// be incorrect if the user wants to see the used SpareParts so in this case
	// every time when something will be changed in the Reparation we just add
	// one String to the log maybe the current time included)
	private List<String> logs;

	/**
	 * This List is to store the temporary SpareParts for the Reparation. The
	 * Object from this List will be deleted if the update() method was called
	 * (the Objects from here will be replaced to the SparePartHistory)
	 * 
	 * @author Csaba Tarkanyi
	 */
	private List<SparePartUse> temp;

	/**
	 * @param uniqueID -
	 *            here: 7 digits (only numbers)
	 * @param startDate -
	 *            start date of the Reparation
	 * @param description -
	 *            for the Reparation
	 * @param machine -
	 *            which is belongs to the Reparation
	 * 
	 * @author Csaba Tarkanyi
	 */
	public Reparation(long uniqueID, GregorianCalendar startDate,
			String description, Machine machine) {
		this.uniqueID = uniqueID;
		this.startDate = startDate;
		this.description = description;
		this.machine = machine;

		sparePartHistories = new ArrayList<SparePartHistory>();
		temp = new ArrayList<SparePartUse>();
		this.machine.addReparation(this);
		logs = new ArrayList<String>();
	}

	/**
	 * @return the uniqueID
	 * 
	 * @author Csaba Tarkanyi
	 */
	public long getUniqueID() {
		return uniqueID;
	}

	/**
	 * @param uniqueID
	 *            the uniqueID to set
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void setUniqueID(long uniqueID) {
		this.uniqueID = uniqueID;
	}

	/**
	 * @return the startDate
	 * 
	 * @author Csaba Tarkanyi
	 */
	public GregorianCalendar getStartDate() {
		return startDate;
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void setStartDate(GregorianCalendar startDate) {
		this.startDate = startDate;
	}

	/**
	 * @return the endDate
	 * 
	 * @author Csaba Tarkanyi
	 */
	public GregorianCalendar getEndDate() {
		return endDate;
	}

	/**
	 * @return the description
	 * 
	 * @author Csaba Tarkanyi
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	// Link-Methods start

	/**
	 * Adds a SparePartHistory to this Reparation. <br/>(and sets the Reparation
	 * of the SparePartHistory to this Reparation). <br />
	 * Requires: SparePartHistory != null
	 * 
	 * @author Csaba Tarkanyi
	 */
	void addSparePartHistory(SparePartHistory sparePartHistory) {
		if (!sparePartHistories.contains(sparePartHistory)) {
			sparePartHistories.add(sparePartHistory);
			sparePartHistory.setReparation(this);
		}
	}

	/**
	 * Removes a SparePartHistory from this Reparation <br />
	 * (and sets the Reparation of the SparePartHistory to null). <br />
	 * Requires: SparePartHistory != null
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void removeSparePartHistory(SparePartHistory sparePartHistory) {
		if (sparePartHistories.contains(sparePartHistory)) {
			sparePartHistories.remove(sparePartHistory);
			sparePartHistory.setReparation(null);
		}
	}

	/**
	 * Returns to a List with all the SparePartHistories.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<SparePartHistory> getSparePartHistories() {
		/*
		 * Returns to a List with all the SparePartHistories.
		 */
		return sparePartHistories;
	}

	/**
	 * @return the machine
	 * 
	 * @author Csaba Tarkanyi
	 */
	public Machine getMachine() {
		return machine;
	}

	/**
	 * Sets the Reparation's Machine<br />
	 * Requires: machine != null
	 * 
	 * @param machine
	 *            the machine to set
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void setMachine(Machine machine) {
		this.machine = machine;
	}

	// Link-Methods end

	// Other-Methods start

	/**
	 * Returns a String of logs.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public String getLog() {
		String logFull = "";

		if (logs.size() == 0) {
			logFull = "no log";
		} else {
			for (String l : logs) {
				logFull += l
						+ "\n----------------------8<----------------------\n";
			}
		}
		return "log for Reparation (" + getUniqueID() + "): \n\n" + logFull;
	}

	/**
	 * Adds a new Log with the current time.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void addLog(String newLog) {
		logs.add(newLog
				+ "\nLog Time: ["
				+ MainFrame.getSimpleDateFormat().format(
						new GregorianCalendar().getTime()) + "]");
	}

	/**
	 * Searches for an existing SparePartUse in the temp List.
	 * 
	 * @param spu
	 *            the SparePartUse for the search
	 * @return -to the SparePartUse if the SparePartUse exists already<br />
	 *         -else to null.
	 * 
	 * @author Csaba Tarkanyi
	 */
	private SparePartUse findSparePartUse(SparePartUse spu) {
		for (SparePartUse sput : getTempSparePartUses()) {
			if (sput.getSparePart().equals(spu.getSparePart())) {
				return sput;
			}
		}
		return null;
	}

	/**
	 * Adds a SparePartUse to the temporary List. If the SparePart was already
	 * in the temp List it will add the quantity to the existing SparePartUse
	 * else makes a new entry in the temp List.
	 * 
	 * @throws -
	 *             a RuntimeException if the quantity is less than 1.
	 * 
	 * @throws -
	 *             a RuntimeException if there is no enough SparePart to remove
	 *             from the inventory.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void addTempSparePartUse(SparePartUse spu) {
		// If the quantity less than 1 throws a RuntimeException.
		if (spu.getQuantity() < 1) {
			throw new RuntimeException("The quantity can not be below 1!");
		}
		// Checks the inventory status.
		if (spu.getSparePart().getUnit() < spu.getQuantity()) {
			throw new RuntimeException("There is no enough '"
					+ spu.getSparePart().getName()
					+ "' spare part on stock! Current amount is "
					+ spu.getSparePart().getUnit());
		}
		// Tries to find the SparePartUse in the temp List.
		SparePartUse equal = findSparePartUse(spu);
		// If the SparePartUse exists already, just takes more quantity.
		if (equal != null) {
			equal.setQuantity(equal.getQuantity() + spu.getQuantity());
		} else {
			// Adds the SparePartUse to the temp List if it is "unique".
			temp.add(spu);
		}
	}

	/**
	 * Removes a SparePartUse from the temporary List. Takes less SparePartUse
	 * from the inventory or deletes the entry if the new quantity is zero.
	 * 
	 * @throws -
	 *             a RuntimeException if the quantity is less than 1.
	 * 
	 * @throws -
	 *             a RuntimeException if the new quantity for the existing
	 *             SparePartUse is positive (to much to remove)
	 * 
	 * @throws -
	 *             a RuntimeException if the SparePartUse does not exist in the
	 *             temp List.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void removeTempSparePartUse(SparePartUse spu) {
		// If the quantity less than 1 throws a RuntimeException.
		if (spu.getQuantity() < 1) {
			throw new RuntimeException("The quantity can not be below 1!");
		}
		// Tries to find the SparePartUse in the temp List.
		SparePartUse equal = findSparePartUse(spu);
		// The SparePartUse should exists else throws a RuntimeException.
		if (equal != null) {
			// Just takes less quantity.
			int correction = equal.getQuantity() - spu.getQuantity();
			// Removes the SparePartUse if its quantity zero.
			if (correction == 0) {
				temp.remove(equal);
			}
			// If the quantity is too much to remove.
			else if (correction < 1) {
				throw new RuntimeException(
						"\nThe quantity is too much to remove! \n"
								+ "The new quantity of the new SparePartUse (with "
								+ spu.getSparePart().getName()
								+ " SparePart) would be " + correction);
			}
			// If the quantity is right to remove.
			else if (correction > 1) {
				equal.setQuantity(correction);
			}
		} else {
			throw new RuntimeException(
					"The SparePartUse does not exist in the Temp List!");
		}
	}

	/**
	 * @return to a List of SparePartUse.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<SparePartUse> getTempSparePartUses() {
		return temp;
	}

	/**
	 * Updates the SparePartHistory by adding (adding->creating SparePartHistory
	 * for all the SparePartUses) all the elements from the temporary List of
	 * SparePartUse to the List of SparePartHistory. At the same time it takes
	 * the SparePart form the inventory.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void update() {
		for (SparePartUse spu : getTempSparePartUses()) {
			// Creates History for all the SparePart in the temp List one by one
			spu.getSparePart().createSparePartHistory(this, spu);
			// Takes the SparePart form the inventory.
			spu.getSparePart().setUnit(
					spu.getSparePart().getUnit() - spu.getQuantity());
			// Creates a Log for every SparePartUse (including the SparePartUses
			// of RepairTypes!)
			addLog(getSparePartUseLogPrint(spu));
		}
		// After creating the histories and taking the SpareParts clears the
		// temp List.
		temp.clear();
	}

	/**
	 * Makes a "LogString" for the SparePartUse.
	 * 
	 * @param rt -
	 *            makes the String for this SparePartUse
	 * @return to the "logString"
	 * 
	 * @author Csaba Tarkanyi
	 */
	private String getSparePartUseLogPrint(SparePartUse spu) {
		return "\n<<<<<<<<<<<<<<One Spare Part Use>>>>>>>>>>>>>>"
				+ "\n<<<<<<From The Merged List Of RepairTypes>>>>>"
				+ "\n<<<<<<<<<<<And_Single_SparePartUses>>>>>>>>>>>\n"
				+ "\n<Add:>\n" + "SparePartUse:\n"
				+ "-Amount of Used Spare Part: " + spu.getQuantity()
				+ "\n-Spare Part: " + "\n -UniqueID: "
				+ spu.getSparePart().getUniqueID() + "\n -StorageID: "
				+ spu.getSparePart().getStorageID() + "\n -DrawID: "
				+ spu.getSparePart().getDrawID() + "\n -BoxID: "
				+ spu.getSparePart().getBoxID() + "\n -Name: "
				+ spu.getSparePart().getName() + "\n -Unit: "
				+ spu.getSparePart().getUnit() + "\n -Preset Limit (in days): "
				+ spu.getSparePart().getPresetLimit()
				+ "\n -Average Delivery Time: "
				+ spu.getSparePart().getAvgDeliveryTime()
				+ "\n -Spaecial Note: " + spu.getSparePart().getSpecialNote();
	}

	/**
	 * Makes a "LogString" for the RepairType.
	 * 
	 * @param rt -
	 *            makes the String for this RepairType
	 * @return to the "logString"
	 * 
	 * @author Csaba Tarkanyi
	 */
	private String getReparirTypeLogPrint(RepairType rt) {
		String newLog = "";
		int counter = 1;
		// Creates String for the SparePartUses
		for (SparePartUse spu : rt.getSparePartUses()) {
			newLog += "\nSparePartUse nr." + counter + ":\n"
					+ "-Amount of Used Spare Part: " + spu.getQuantity()
					+ "\n-Spare Part: " + "\n -UniqueID: "
					+ spu.getSparePart().getUniqueID() + "\n -StorageID: "
					+ spu.getSparePart().getStorageID() + "\n -DrawID: "
					+ spu.getSparePart().getDrawID() + "\n -BoxID: "
					+ spu.getSparePart().getBoxID() + "\n -Name: "
					+ spu.getSparePart().getName() + "\n -Unit: "
					+ spu.getSparePart().getUnit()
					+ "\n -Preset Limit (in days): "
					+ spu.getSparePart().getPresetLimit()
					+ "\n -Average Delivery Time: "
					+ spu.getSparePart().getAvgDeliveryTime()
					+ "\n -Spaecial Note: "
					+ spu.getSparePart().getSpecialNote()
					+ "\n______________________________________________\n";
			counter++;
		}
		// Creates String for the Included RepairType(s)
		for (RepairType irt : rt.getRepairTypes()) {
			newLog += "------------------<Included>-------------Start\n"
					// Recursion (calls the method itself inside its the body)
					+ getReparirTypeLogPrint(irt)
					+ "------------------<Included>---------------End\n";
		}
		return "\nRepairType:" + "\n -ID: " + rt.getID() + "\n -Name: "
				+ rt.getName() + "\n -Description: " + rt.getDescription()
				+ "\n" + newLog;
	}

	/**
	 * Adds all the SparePartUses from the RepairType to the temp List and
	 * creates a new log.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void addRepairType(RepairType rt) {
		addLog("<Add>: " + getReparirTypeLogPrint(rt));
		temp.addAll(rt.getSparePartUses());
	}

	/**
	 * Calculates the duration of reparation by getting the startDate and the
	 * endDate.<br />
	 * Requires: startDate != null.
	 * 
	 * @return -if the endDate == null: to the duration of current and start
	 *         date in milliseconds.<br />
	 *         -else to the difference of endDate and startDate in milliseconds.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public long getDuration() {
		if (endDate == null) {
			// Returns to the difference of current and start date.
			return Calendar.getInstance().getTimeInMillis()
					- startDate.getTimeInMillis();
		} else
			// Returns to the different of the two GregorianCalendars.
			return endDate.getTimeInMillis() - startDate.getTimeInMillis();
	}

	/**
	 * Calculates the duration between start and end.
	 * 
	 * @param start
	 * @param end
	 * @return to the duration
	 * 
	 * @author Csaba Tarkanyi
	 */
	public long getDuration(GregorianCalendar start, GregorianCalendar end) {
		if (start.after(end)) {
			throw new RuntimeException("wrong dates!");
		}
		return end.getTimeInMillis() - start.getTimeInMillis();
	}

	/**
	 * Creates a String by checking a long and adding a String.
	 * <p> () -> inside the round brackets is the created String.<br />
	 * If the long is 1 -> (lng+_+str+_) <br />
	 * If the long is zero -> () <br />
	 * Else -> (lng+_+str+s+_)
	 * 
	 * @return to the created String.
	 * 
	 * @author Csaba Tarkanyi
	 */
	private String getDurStr(long lng, String str) {
		if (lng == 1)
			return lng + "" + str + " ";
		else if (lng == 0)
			return "";
		else
			return lng + "" + str + "s ";
	}

	/**
	 * Creates a Nice-And-Pretty String for the Duration.
	 * 
	 * @return to the created String.
	 * 
	 * @author Csaba Tarkanyi
	 */
	private String getNiceAndPrettyDuartionPrint(long week, long day,
			long hour, long minute, long second) {
		String[] st = { "week", "day", "hour", "minute", "second" };

		return getDurStr(week, st[0]) + getDurStr(day, st[1])
				+ getDurStr(hour, st[2]) + getDurStr(minute, st[3])
				+ getDurStr(second, st[4]);
	}

	/**
	 * Makes a String by getting a long, splits it into week, day, hour, minute,
	 * second.
	 * 
	 * @param milliseconds -
	 *            is the long for splitting.
	 * @return to the Nice-And-Pretty String <br />
	 *         (1 week 4 days 22 hours 45 minutes 51 seconds)
	 * 
	 * @author Csaba Tarkanyi
	 */
	public String makeNiceDurPrint(long milliseconds) {

		int ONE_SECOND = 1000;
		int ONE_MINUTE = 60 * ONE_SECOND;
		int ONE_HOUR = 60 * ONE_MINUTE;
		long ONE_DAY = 24 * ONE_HOUR;
		long ONE_WEEK = 7 * ONE_DAY;
		// Calculating the duration in weeks, days, hours, minutes, seconds.
		long week = milliseconds / ONE_WEEK;
		milliseconds -= week * ONE_WEEK;
		long day = milliseconds / ONE_DAY;
		milliseconds -= day * ONE_DAY;
		long hour = milliseconds / ONE_HOUR;
		milliseconds -= hour * ONE_HOUR;
		long minute = milliseconds / ONE_MINUTE;
		milliseconds -= minute * ONE_MINUTE;
		long second = milliseconds / ONE_SECOND;
		milliseconds -= second * ONE_SECOND;

		return getNiceAndPrettyDuartionPrint(week, day, hour, minute, second);
	}

	/**
	 * Gets the Duration in week, day, hour, minute, second.
	 * 
	 * @return to a Nice-And-Pretty String for the Duration.<br />
	 *         (example: 1 week 4 days 22 hours 45 minutes 51 seconds)
	 * 
	 * @author Csaba Tarkanyi
	 */
	public String getDurationDetail() {
		return makeNiceDurPrint(getDuration());
	}

	/**
	 * Calculates the idle time within the requested month. If the repair hasn't
	 * finished then the current time is used as the end time.
	 * 
	 * @param year
	 *            The requested year.
	 * @param month
	 *            The requested month.
	 * @return The idle time of the repair in the requested month in number of
	 *         milliseconds.
	 */
	public long getIdleTime(GregorianCalendar date) {
		int year = date.get(GregorianCalendar.YEAR);// date's year
		int month = date.get(GregorianCalendar.MONTH);// date's month
		GregorianCalendar calcStartTime; // Time used in the calculation.
		GregorianCalendar calcEndTime; // Time used in the calculation.
		GregorianCalendar compEndTime; // The end time to use in compare.

		// If the repair hasn't finished then set to the current time.
		if (endDate == null)
			compEndTime = new GregorianCalendar();
		else
			compEndTime = endDate;

		// Clone the start and end times and change to the requested year and
		// month.
		GregorianCalendar tempStartTime = (GregorianCalendar) startDate.clone();
		tempStartTime.set(GregorianCalendar.YEAR, year);
		tempStartTime.set(GregorianCalendar.MONTH, month);
		GregorianCalendar tempEndTime = (GregorianCalendar) compEndTime.clone();
		tempEndTime.set(GregorianCalendar.YEAR, year);
		tempEndTime.set(GregorianCalendar.MONTH, month);

		// If end time is equal or before start time, or start time is
		// equal or after end time, then there was no idle time the requested
		// month and return 0;
		if ((tempStartTime.after(compEndTime) || tempEndTime.before(startDate))
				|| (tempStartTime.equals(compEndTime) || tempEndTime
						.equals(startDate)))
			return 0;

		// Find the year and month of the start time.
		int startTimeYear = startDate.get(GregorianCalendar.YEAR);
		int startTimeMonth = startDate.get(GregorianCalendar.MONTH);

		// If the year and month are the same in the start time as the requested
		// then use that as time to calculate, else set to the start of the
		// requested month.
		if ((startTimeYear == year) && (startTimeMonth == month))
			calcStartTime = startDate;
		else
			calcStartTime = new GregorianCalendar(year, month, 1);

		// Find the year and month of the end time.
		int endTimeYear = compEndTime.get(GregorianCalendar.YEAR);
		int endTimeMonth = compEndTime.get(GregorianCalendar.MONTH);

		// If the year and month are the same in the start time as the requested
		// then use that as the time to calculate, else set to the end of the
		// requested month.
		if ((endTimeYear == year) && (endTimeMonth == month))
			calcEndTime = compEndTime;
		else {
			calcEndTime = new GregorianCalendar(year, month, 1);
			calcEndTime.set(GregorianCalendar.DATE, calcEndTime
					.getMaximum(GregorianCalendar.DATE) + 1);
		}

		// Calculate the milliseconds between the two dates which should be in
		// the same month.
		return calcEndTime.getTimeInMillis() - calcStartTime.getTimeInMillis();
	}

	/**
	 * Gets the SparePartHistories from the reparation and returns to a
	 * Nice-And-Pretty String.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public String getSparePartHistoryPrint(SimpleDateFormat dateFormat) {
		String text = "";
		// If there is no SparePartHistory
		if (getSparePartHistories() == null) {
			return "No SparePartHistory for '" + getUniqueID()
					+ "' Reparation.";
		} else {
			int counter = 1;
			// Creates String from the SparePartHitories
			for (SparePartHistory sph : getSparePartHistories()) {
				text += "\nSpare Part History nr."
						+ counter
						+ "\n-Date: "
						+ dateFormat.format(sph.getDate().getTime())
						+ "\n-Amount of Used Spare Part: "
						+ sph.getSparePartUse().getQuantity()
						+ "\n-Spare Part: "
						+ "\n -UniqueID: "
						+ sph.getSparePartUse().getSparePart().getUniqueID()
						+ "\n -StorageID: "
						+ sph.getSparePartUse().getSparePart().getStorageID()
						+ "\n -DrawID: "
						+ sph.getSparePartUse().getSparePart().getDrawID()
						+ "\n -BoxID: "
						+ sph.getSparePartUse().getSparePart().getBoxID()
						+ "\n -Name: "
						+ sph.getSparePartUse().getSparePart().getName()
						+ "\n -Unit: "
						+ sph.getSparePartUse().getSparePart().getUnit()
						+ "\n -Preset Limit (in days): "
						+ sph.getSparePartUse().getSparePart().getPresetLimit()
						+ "\n -Average Delivery Time: "
						+ sph.getSparePartUse().getSparePart()
								.getAvgDeliveryTime() + "\n -Spaecial Note: "
						+ sph.getSparePartUse().getSparePart().getSpecialNote()
						+ "\n_____________________________________________";
				counter++;
			}
			// String for the endDate.
			String endDate = "";
			// Checks is endDate null.
			if (getEndDate() == null)
				endDate = "<Not Finished>";
			else
				endDate = dateFormat.format(getEndDate().getTime());

			return "//////////////////////////////////////////////\n"
					+ "///////////<<Reparation ("
					+ getUniqueID()
					+ ")>>///////////\n"
					+ "//////////////////////////////////////////////\n"
					+ "\nReparation Details:\n"
					+ "\n-UniqueID: "
					+ getUniqueID()
					+ "\n-Start Date: "
					+ dateFormat.format(getStartDate().getTime())
					+ "\n-EndDate: "
					+ endDate
					+ "\n-Machine: "
					+ "\n -ID: "
					+ getMachine().getID()
					+ "\n -Name: "
					+ getMachine().getName()
					+ "\n -Description: "
					+ getMachine().getDescription()
					+ "\n-Description: "
					+ getDescription()
					+ "\n-Duration:\n< "
					+ getDurationDetail()
					+ ">\n\n  -----------------------------------------\n"
					+ "-//////////Spare Part History List\\\\\\\\\\\\\\\\\\\\-"
					+ "\n_____________________________________________\n"
					+ text
					+ "\n\n-\\\\\\\\\\\\\\\\\\\\Spare Part History List//////////-"
					+ "\n  -----------------------------------------\n\n\n"
					+ "Date of Printing: \n"
					+ dateFormat.format(new GregorianCalendar().getTime())
					+ "\n----------------------8<----------------------\n";
		}
	}

	/**
	 * Finishes a Reparation by setting the endDate to the current time.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void endReparation() {
		/*
		 * Sets the endDate with the current time.
		 */
		this.endDate = new GregorianCalendar();
	}

	/**
	 * Temporary: just for testing.
	 */
	public void endReparation(GregorianCalendar gc) {
		this.endDate = gc;
	}

	/**
	 * TODO Checks the amount of the SparePart before a certain amount will be
	 * used for a Reparation by getting the Inventory Unit of the SparePart and
	 * adding the the Quantity form the SparePartUse to that.<br />
	 * (minus-quantity means taking from the inventory and adding to the
	 * Reparation,<br />
	 * plus-quantity means adding to the inventory and maybe taking from the
	 * Reparation)
	 * 
	 * @param spu
	 *            is the SparePartUse with the Quantity and the SparePart.
	 * 
	 * @throws a
	 *             RuntimeException if the taken Quantity is more than the
	 *             SparePart-unit (it is impossible to take more than we have on
	 *             stock)
	 * 
	 * @author Csaba Tarkanyi
	 */
	public void checkAmount(SparePartUse spu) {
		if (spu.getSparePart().getUnit() + spu.getQuantity() < 0) {
			throw new RuntimeException("There is no enough '"
					+ spu.getSparePart().getName()
					+ "' spare part on stock! Current amount is "
					+ spu.getSparePart().getUnit());
		}
	}

	// Other-Methods end

	/**
	 * Returns to a String with the necessary parameters, what we want to see.<br />
	 * 
	 * This toString method overrides the inherited toString method from the
	 * Object class.
	 * 
	 * @author Csaba Tarkanyi
	 */
	@Override
	public String toString() {
		if (endDate == null) {
			return uniqueID + " " + startDate.getTime()
					+ " (--------notFinished--------) " + description + " "
					+ machine.getName();
		} else
			return uniqueID + " " + startDate.getTime() + " "
					+ endDate.getTime() + " " + description + " "
					+ machine.getName();
	}
}